package com.miyake.dsp.fitting.ui;

import java.awt.Dimension;
import java.util.List;

import com.miyake.dsp.fitting.FittingSetting;
import com.miyake.dsp.ui.DspTextField;
import com.miyake.dsp.ui.DspWidget;

public class FittingSettingUI extends DspWidget{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5752295318013227187L;
	private FittingSetting setting;

	public FittingSettingUI(FittingSetting setting) {
		this.setName("Fitting Setting");
		this.setting = setting;
		createStart(setting.getStart());
		createStop(setting.getStop());
		createPoint(setting.getPoint());
		createFormula(setting.getFormula());
		createInitial(setting);
		createMaskStart(setting.getMaskStart());
		createMaskStop(setting.getMaskStop());
	}
	
	private void createInitial(final FittingSetting setting) {
		for (int i = 0; i < setting.getInitial().length; i++) {
			DspTextField text = new DspTextField("x(" + (i+1) + ")");
			text.setValue(setting.getInitial()[i].toString());
			//text.setPreferredSize(new Dimension(400, 32));
			this.add(text);
			
			final Integer index = new Integer(i);
			text.addHandler(new DspWidgetHandler() {
				@Override
				public void onChange(String value) {
					setting.getInitial()[index]= Double.valueOf(value);
					fireChange(value);
				}
			});	
		}
	}

	private void createFormula(String formula) {
		DspTextField text = new DspTextField("Formula");
		text.setValue(formula);
		text.setPreferredSize(new Dimension(400, 32));
		this.add(text);
		
		text.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				setting.setFormula(value);
				fireChange(value);
			}
		});	
	}

	private void createStart(int start) {
		DspTextField text = new DspTextField("Start");
		text.setValue(String.valueOf(start));
		this.add(text);
		
		text.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				setting.setStart(Integer.valueOf(value));
				fireChange(value);
			}
		});	
	}
	
	private void createStop(int stop) {
		DspTextField text = new DspTextField("Stop");
		text.setValue(String.valueOf(stop));
		this.add(text);
		
		text.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				setting.setStop(Integer.valueOf(value));
				fireChange(value);
			}
		});	
	}
	
	private void createPoint(int point) {
		DspTextField text = new DspTextField("Point");
		text.setValue(String.valueOf(point));
		this.add(text);
		
		text.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				setting.setPoint(Integer.valueOf(value));
				fireChange(value);
			}
		});	
	}
	
	private void createMaskStart(int maskStart) {
		DspTextField text = new DspTextField("Mask Start");
		text.setValue(String.valueOf(maskStart));
		this.add(text);
		
		text.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				setting.setMaskStart(Integer.valueOf(value));
				fireChange(value);
			}
		});	
	}
	
	private void createMaskStop(int maskStop) {
		DspTextField text = new DspTextField("Mask Stop");
		text.setValue(String.valueOf(maskStop));
		this.add(text);
		
		text.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				setting.setMaskStop(Integer.valueOf(value));
				fireChange(value);
			}
		});	
	}
}
